package cn.practice.weibopractice.controller;

import cn.practice.weibopractice.common.response.JsonResult;
import cn.practice.weibopractice.common.response.StatusCode;
import cn.practice.weibopractice.pojo.dto.UserLoginDTO;
import cn.practice.weibopractice.pojo.dto.UserRegDTO;
import cn.practice.weibopractice.pojo.entity.User;
import cn.practice.weibopractice.pojo.mapper.UserMapper;
import cn.practice.weibopractice.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    // 自动装配
    @Autowired
    private UserMapper userMapper;

    // 用户注册
    @PostMapping("reg")
    @ApiOperation("用户注册")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO) {

        log.debug("UserRegDTO : " + userRegDTO);
        //
        String username = userRegDTO.getUsername();
        // 查询接口
        UserVO userVO = userMapper.selectByUsername(username);

        if(userVO != null){
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXIST);
        }

        // 注册
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreated(new Date());

        userMapper.insertUser(user);
        return JsonResult.success();
    }

    // 登录
    @PostMapping("login")
    @ApiOperation("用户登录")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        log.debug("UserLoginDTO :" + userLoginDTO);

        String username = userLoginDTO.getUsername();
        UserVO userVO = userMapper.selectByUsername(username);
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }

        if (userVO.getPassword().equals(userLoginDTO.getPassword())){
            // 登录成功
            // 存储用户信息
            session.setAttribute("user", userVO);
            return JsonResult.success(userVO);
        }

        return new JsonResult(StatusCode.PASSWORD_ERROR);
    }

    // 获取当前用户
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户")
    public JsonResult currentUser(@ApiIgnore HttpSession session) {
        log.debug("session : " + session);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //
        return JsonResult.success(userVO);
    }

    // 退出登录
    @GetMapping("logout")
    @ApiOperation("退出登录")
    public JsonResult logout(@ApiIgnore HttpSession session) {
        log.debug("session : " + session);
        // 清空会话
        session.removeAttribute("user");
        session.invalidate(); // 销毁会话
        return JsonResult.success();
    }





}
